package com.gold.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.gold.model.Customer;
import com.gold.service.CustomerService;
import com.gold.util.BaseController;
import com.gold.util.GridPage;
import com.gold.util.Page;

@Controller
@RequestMapping("customer")
public class CustomerContorller extends BaseController{
	
	@Resource
	CustomerService customerService;
	
	@RequestMapping("")
	public ModelAndView toCustomerView(){
		ModelAndView mo=new ModelAndView();
		mo.setViewName("customer");
		return mo;
	}
	
	@RequestMapping("/customerGrid")
	@ResponseBody
	public GridPage<Customer> getGrid(HttpServletRequest request,Customer customer){
		
		int page=this.getParamInt(request, "page", 1);
		int rows=this.getParamInt(request, "pageSize", 10);
		
		Page<Customer> rtPage=new Page<Customer>();

        rtPage.setQueryParam(customer);
        rtPage.setPageSize(rows);
        rtPage.setPageNo(page);
		
        rtPage=customerService.findByCondition(rtPage);
        
		return new GridPage<Customer>(rtPage);
	}
	
	@RequestMapping(value="/save")
    @ResponseBody
    public JSONObject save(HttpServletRequest request,Customer customer) {
		JSONObject jo=new JSONObject();
		boolean b=false;
		try {
			if(customer.getcId()==null)//说明是添加
				b=customerService.save(customer);
			else if(customer.getcId()>0)//说明是修改
				b=customerService.update(customer);
			jo.put("success", b);
			if(b){
				jo.put("msg","保存成功");
			}else{
				jo.put("msg","保存失败");
			}
		} catch (Exception e) {
			jo.put("msg", "发生了异常");
		}
		return jo;
	}

}
